public class BinarryTree {

    class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;//根结点
    public int useSize; //定义计数器
    public int useSize2;

    public void creatTree() {
        TreeNode node = new TreeNode('A');
        root = node;
        TreeNode node1 = new TreeNode('B');
        TreeNode node2 = new TreeNode('C');
        TreeNode node3 = new TreeNode('D');
        TreeNode node4 = new TreeNode('E');
        TreeNode node5 = new TreeNode('F');
        TreeNode node6 = new TreeNode('G');
        TreeNode node7 = new TreeNode('H');
        //连接结点
        node.left = node1;
        node.right = node2;

        node1.left = node3;
        node1.right = node4;
        node4.right = node7;

        node2.left = node5;
        node2.right = node6;

    }
    /**
     * 前序遍历
     */
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * 中序遍历
     */
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val+" ");
    }
    /**
     * 后序遍历
     */
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val+" ");
    }
    /**
     * 求二叉树结点的个数
     */
    public void size1(TreeNode root) {
       //使用计数器
       if(root == null) {
           return;
       }
       useSize++;
        size1(root.left);
        size1(root.right);
    }
    public int size2(TreeNode root) {
        //利用子问题求解
        if(root == null) {
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }
    /**
     * 求叶子节点的个数
     */
    public void getLeafNodeCount(TreeNode root) {
        //使用计数器
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            useSize2++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left)+ getLeafNodeCount2(root.left);
    }
    /**
     * 求第k层节点的个数
     */
    public int getKLeafNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLeafNodeCount(root.left,k-1)+getKLeafNodeCount(root.right,k-1);
    }
    /**
     * 求二叉树的高度/深度
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight)+1;
    }
    /**
     * 查找val的值是否存在
     * 存在，返回该结点
     * 不存在，返回null
     */
    public TreeNode find(TreeNode root,int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftnode = find(root.left,val);
        if(leftnode != null) {
            return leftnode;
        }
        TreeNode rightnode = find(root.right,val);
        if(rightnode != null) {
            return rightnode;
        }
        return null;
    }
    




}
